package financier.jfz.com.babycare;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.table.TableData;
import com.jfz.quick.core.db.LightOrmHelper;
import com.jfz.quick.core.db.LightPrimaryKey;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import financier.jfz.com.babycare.base.BaseActivity;
import financier.jfz.com.babycare.base.Callback;
import financier.jfz.com.babycare.bean.AddItemBean;
import financier.jfz.com.babycare.bean.BaseModle;
import financier.jfz.com.babycare.bean.TableMsgDataBean;
import financier.jfz.com.babycare.bean.TableRowData;
import financier.jfz.com.babycare.dialog.EditeDialog;

public class TableActivity extends BaseActivity {
    public static final String MSG = "msg",MODLE="modle";
    BaseModle baseModle;
    EditeDialog editeDialog;
    SmartTable smartTable;
    TableData<TableRowData> mTableData;
    TableMsgDataBean msgDataBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table);
        initUI();
    }

    private void initUI() {
        msgDataBean= (TableMsgDataBean) getIntent().getSerializableExtra(MSG);
        getToolbar().setRightTitleClickListener("保存", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveData();
            }
        });
        getToolbar().setMainTitle(msgDataBean.MsgName+"数据统计");
        findViewById(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (editeDialog == null)
                    editeDialog = new EditeDialog();
                editeDialog.show(TableActivity.this, baseModle.getData(), new Callback() {
                    @Override
                    public void ececute(Object... obj) {
                        TableRowData itemData= (TableRowData) obj[0];
                        itemData.customerId=msgDataBean.modleId;
                       List<TableRowData> argList= mTableData.getT();
                        argList.add(itemData);
                        mTableData.setT(argList);
                        smartTable.notifyDataChanged();
                    }
                });
            }
        });
        smartTable = findViewById(R.id.table);
        setTestUI(smartTable);

    }

    private void saveData() {
        List<TableRowData> mListData =  mTableData.getT();
        if(mListData.size()>0){
           for(TableRowData item:mListData){
               Map<String,String> map=new HashMap<>();
               int size=baseModle.getData().size();
               for(int i=0;i<size;i++){
                 TableRowData.TableItemData itemData= item.getData(i);
                   map.put(itemData.key,itemData.labData);
               }
               TableMsgDataBean.SaveItemTableData itemTableData=new TableMsgDataBean.SaveItemTableData();
               itemTableData.data=map;
//               itemTableData.id=item.lab0
           }
        }

//        msgDataBean.rowData.addAll(data);
        LightOrmHelper.getInstent().save(msgDataBean);
    }

    private void setTestUI(SmartTable smartTable) {
        baseModle = (BaseModle) getIntent().getSerializableExtra(MODLE);
        if (baseModle == null)
            return;
        praseTableData();
        smartTable.setTableData(mTableData);
        smartTable.setZoom(true);
        smartTable.getConfig().setMinTableWidth(QlightUnit.getDisplay().widthPixels); //设置最小宽度
    }

    void praseTableData() {
        List<Column> columns = new ArrayList<>();
        Column item;
        int index=0;
        for (AddItemBean mAddItemBean : baseModle.getData()) {
            String name="lab"+index+".labData";
            item = new Column(mAddItemBean.name, name);
            columns.add(item);
            index++;
        }
       mTableData= new TableData<>(baseModle.getName(), new ArrayList<TableRowData>() , columns);
        mTableData.setOnItemClickListener(new TableData.OnItemClickListener() {
            @Override
            public void onClick(Column column, String value, Object o, int col, int row) {

            }
        });
//        List<TableRowData> mListData=new ArrayList<>();
//        List<Map<String, String>> mapList= msgDataBean.data;
//        if(mapList!=null){
//            int size=mapList.size();
//            for(int i=0;i<size;i++){
//
//                TableRowData tableRowData=new TableRowData();
//                TableRowData.TableItemData  tableItemData=new TableRowData.TableItemData();
//                tableItemData.id
//                tableRowData.setData();
//            }
//        }
    }

    @Override
    protected void backPress() {
       new  AlertDialog.Builder(this).setTitle("提示").setMessage("尚未保存確定退出？")
               .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialogInterface, int i) {
                       TableActivity.super.backPress();
                   }
               }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialogInterface, int i) {

           }
       }).create().show();

    }

    ///        Column c1 = new Column("测试标题1", "lab1.labData");
//////        c1.setFixed(true);
////        Column c2 = new Column("测试标题2", "lab1.labData");
////        Column c3 = new Column("测试标题3", "lab1.labData");
////        Column comb = new Column("组合", c1, c2);
////        columns.add(c1);
////        columns.add(c2);
////        columns.add(c3);
////        columns.add(comb);
////        columns.add(c1);
    //                List<TableRowData> mList = new ArrayList<>();
//                mList.add(new TableRowData());
//                smartTable.addData(mList,false);






}
